//
//  RegisterVC.swift
//  物料商行
//
//  Created by HaoDong Hong on 2017/8/13.
//  Copyright © 2017年 HaoDong Hong. All rights reserved.
//

import UIKit

protocol Delegate: class {
    func getBackPhone(_ phone:String)
}

class RegisterVC: BaseVC {
    
    @IBOutlet weak var phoneTF: HDUITextField!
    @IBOutlet weak var passwordTF: HDUITextField!

    weak var delegate: Delegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    @IBAction func register(_ button: TransitionButton!) {
        view.endEditing(true)
        guard phoneTF.text != "", phoneTF.text!.characters.count == 11 else {
            phoneTF.animateLineView()
            return
        }
        guard passwordTF.text != "", passwordTF.text!.characters.count > 5 else {
            passwordTF.animateLineView()
            return
        }
        button.startAnimation()
        helper.register(with: phoneTF.text!, password: passwordTF.text!) { (error) in
            button.stopAnimation()
            if error == LoginError.userExist {
                self.showError(message: "用户已存在，请登录")
            } else {
                self.showSuccess(message: "注册成功，请登录")
                self.delegate?.getBackPhone(self.phoneTF.text!)
                self.navigationController?.popViewController(animated: true)
            }
        }
    }
}

